package cn.t.repository.impl.order;

import cn.t.converter.order.RegionConverter;
import cn.t.model.order.RegionDO;
import cn.t.persistence.entity.order.RegionPO;
import cn.t.persistence.mapper.order.RegionMapper;
import cn.t.repository.order.RegionRepository;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 省市区表数据仓库
 */
@Slf4j
@Repository
@AllArgsConstructor
public class RegionRepositoryImpl extends ServiceImpl<RegionMapper, RegionPO> implements RegionRepository {

    private final RegionConverter regionConverter;

    @Override
    public List<RegionDO> getRegions(String parentCode) {
        LambdaQueryWrapper<RegionPO> queryWrapper = new LambdaQueryWrapper<RegionPO>()
                .eq(StringUtils.hasText(parentCode), RegionPO::getParentCode, parentCode)
                .last(" order by sort ");

        return regionConverter.po2Model(this.list(queryWrapper));
    }
}
